<?php
// @formatter:off
/**
 * @file WCLog.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-05 14:30:06 +0000 (Fri, 05 Jul 2013) $
 *
 * $Id: WCLog.php 89 2013-07-05 14:30:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/WCLog.php $
 */

class WCLogLevel {
	const Fatal		= 0;
	const Error		= 1;
	const Warning		= 2;
	const Info		= 3;
	const Debug		= 4;
	const DetailledDebug	= 5;
};
// @formatter:on

/**
 * @class WCLog
 */
class WCLog {
	protected static $_Logs = array();

	protected $_name = false;
	protected $_path = false;
	protected $_ok = false;
	protected $_level = WCLogLevel::Info;

	protected $_writeOnFile = false;
	protected $_writeOnScreen = false;

	protected function __construct($id) {
		global $wcPaths;

		$this->_name = $id;
		$this->_path = "{$wcPaths['logs-directory']}/wc_{$id}_".date('YmdHis').".log";

		if(!is_writable($wcPaths['logs-directory'])) {
			trigger_error(__CLASS__."::".__FUNCTION__.": Logs directory has no read/write permissions.", E_USER_ERROR);
		}

		$this->header();
	}
	public function __destruct() {
		$this->footer();
	}
	public function isOk() {
		return $this->_ok;
	}
	public function level() {
		return $this->_level;
	}
	public function name() {
		return $this->_name;
	}
	public function path() {
		return $this->_path;
	}
	public function set($level = WCLogLevel::Info, $writeOnFile = true, $writeOnScreen = true) {
		global $wcDefaults;

		$this->_writeOnFile = !$wcDefaults["no-logs"] && $writeOnFile ? "realWriteOnFile" : "dummy";
		$this->_writeOnScreen = $writeOnScreen ? "realWriteOnScreen" : "dummy";
		$this->_level = $level;

		$this->_ok = true;

		return $this->isOk();
	}
	public function write($log, $level = WCLogLevel::Info) {
		if($this->isOk()) {
			if($level <= $this->level()) {
				eval("\$this->{$this->_writeOnScreen}(\$log, \$level);");
				eval("\$this->{$this->_writeOnFile}(\$log, \$level);");
			}
		} else {
			trigger_error(__CLASS__."::".__FUNCTION__.": Log is not set yet.", E_USER_ERROR);
		}
	}
	protected function dummy($log, $level) {
		// nothing
	}
	protected function header() {
		global $wcDefaults;
		if(!$wcDefaults["no-logs"]) {
			$aux = str_repeat("-", 80)."\n";
			$aux .= __CLASS__.": Header\n";
			$aux .= "\tDate:  ".date('Y-m-d H:i:s')."\n";
			$aux .= "\tLevel: {$this->level()}\n";
			$aux .= str_repeat("-", 80)."\n";
			file_put_contents($this->path(), $aux, FILE_APPEND);
		}
	}
	protected function footer() {
		global $wcDefaults;
		if(!$wcDefaults["no-logs"]) {
			$aux = str_repeat("-", 80)."\n";
			$aux .= __CLASS__.": Footer\n";
			$aux .= "\tDate:  ".date('Y-m-d H:i:s')."\n";
			$aux .= str_repeat("-", 80)."\n";
			file_put_contents($this->path(), $aux, FILE_APPEND);
		}
	}
	protected function realWriteOnFile($log, $level) {
		$log = preg_replace("/\033([0-9]*)([;]?)(.*?)m/", "", $log);
		file_put_contents($this->path(), $log, FILE_APPEND);
	}
	protected function realWriteOnScreen($log, $level) {
		echo $log;
	}
	/**
	 * @param string $id
	 * @return WCLog
	 */
	public static function & Get($id) {
		if(!isset(WCLog::$_Logs[$id])) {
			WCLog::$_Logs[$id] = new WCLog($id);
		}

		return WCLog::$_Logs[$id];
	}
}
?>